package com.informatica.test.gwt.client;

import com.informatica.test.gwt.shared.FieldVerifier;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyUpEvent;
import com.google.gwt.event.dom.client.KeyUpHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TextBox;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class maven implements EntryPoint {
  /**
   * The message displayed to the user when the server cannot be reached or
   * returns an error.
   */
  private static final String SERVER_ERROR = "An error occurred while "
      + "attempting to contact the server. Please check your network "
      + "connection and try again.";

  /**
   * Create a remote service proxy to talk to the server-side Greeting service.
   */
  private final BetServiceAsync betService = GWT.create(BetService.class);

  /**
   * This is the entry point method.
   */
  public void onModuleLoad() {
	  final Button sendButton = new Button("Send");
		final TextBox userParameter = new TextBox();
		final Label errorLabel = new Label();
		final HTML resultLabel = new HTML();

		// We can add style names to widgets
		sendButton.addStyleName("sendButton");

		// Use RootPanel.get() to get the entire body element
		RootPanel.get("userParameterContainer").add(userParameter);
		RootPanel.get("sendButtonContainer").add(sendButton);
		RootPanel.get("errorLabelContainer").add(errorLabel);
		RootPanel.get("resultContainer").add(resultLabel);

		// Focus the cursor on the name field when the app loads
		userParameter.setFocus(true);

		// Create a handler for the sendButton and nameField
		class MyHandler implements ClickHandler, KeyUpHandler {
			/**
			 * Fired when the user clicks on the sendButton.
			 */
			public void onClick(ClickEvent event) {
				sendNameToServer();
			}

			/**
			 * Fired when the user types in the nameField.
			 */
			public void onKeyUp(KeyUpEvent event) {
				if (event.getNativeKeyCode() == KeyCodes.KEY_ENTER) {
					sendNameToServer();
				}
			}

			/**
			 * Send the name from the nameField to the server and wait for a response.
			 */
			private void sendNameToServer() {
				// First, we validate the input.
				errorLabel.setText("");
				String textToServer = userParameter.getText();
				if (!FieldVerifier.isValidName(textToServer)) {
					errorLabel.setText("Incorrect ");
					return;
				}

				// Then, we send the input to the server.
				//sendButton.setEnabled(false);
				resultLabel.setText("");
				betService.greetServer(textToServer,
						new AsyncCallback<String>() {
							public void onFailure(Throwable caught) {
								// Show the RPC error message to the user
								
								resultLabel
										.addStyleName("serverResponseLabelError");
								resultLabel.setHTML(SERVER_ERROR);
								
							}

							public void onSuccess(String result) {
								resultLabel
										.removeStyleName("serverResponseLabelError");
								resultLabel.setHTML("Results:");

							}
						});
			}
		}

		// Add a handler to send the name to the server
		MyHandler handler = new MyHandler();
		sendButton.addClickHandler(handler);
		userParameter.addKeyUpHandler(handler);
		
  }
  
}